package leetcode.code2038;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public boolean winnerOfGame(String colors) {
		char[] cs = colors.toCharArray();
		int last = cs[0], lasts = 0, count = 1;
		int ans = 0;
		for (int i = 1; i < cs.length; i++) {
			int cur = cs[i];
			if (last == cur) {
				count++;
			} else {
				if (count >= 3) {
					int num = i - lasts - 2;
					ans = last == 'A' ? ans + num : ans - num;
				}
				last = cur;
				lasts = i;
				count = 1;
			}
			last = cur;
		}
		if (count >= 3) {
			int num = cs.length - lasts - 2;
			ans = last == 'A' ? ans + num : ans - num;
		}
		return ans > 0;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(false, this.winnerOfGame("AA"));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(true, this.winnerOfGame("AAABABB"));

	}

	@Override
	public void debug3() {
		HelpDebug.compare(false, this.winnerOfGame("ABBBBBBBAAA"));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

		HelpDebug.compare(false, this.winnerOfGame("AAAABBBB"));
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
